/**
* This file is part of JizyEjb.
*
*    Nome-Programma is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Nome-Programma is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with  JizyEjb.  If not, see <http://www.gnu.org/licenses/>.
*   
*    Copyright 2013 user    
*/
package com.tabuto.jizy.dao.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.tabuto.jizy.dao.ContactDao;
import com.tabuto.jizy.models.AbstractUser;
import com.tabuto.jizy.models.ContactModel;

/**
 * @author user
 *
 */
public class ContactJDBCDao extends AbstractJDBCDao implements ContactDao {

	/* (non-Javadoc)
	 * @see com.tabuto.jizy.dao.ContactDao#addContact(com.tabuto.jizy.models.ContactModel, com.tabuto.jizy.models.AbstractUser)
	 */
	@Override
	public boolean addContact(ContactModel contact, AbstractUser user) {
		int result = 0;
		try{
			Connection c = getConnection();
			
			
			PreparedStatement pstat  = 
					c.prepareStatement(
							JDBCConstant.SQL_P_INSERT_CONTACT);
			
			pstat.setInt( 1, (int) user.getId());
			pstat.setString(2, contact.getName());
			pstat.setString(3, contact.getSurname());
			result = pstat.executeUpdate();
			
			
			c.close();
		} catch (Exception e){
			throw new RuntimeException("An error occours");
		}
		return (result==1);
	}

	/* (non-Javadoc)
	 * @see com.tabuto.jizy.dao.ContactDao#editContact(com.tabuto.jizy.models.ContactModel, com.tabuto.jizy.models.AbstractUser)
	 */
	@Override
	public boolean editContact(ContactModel toEdit, AbstractUser user) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see com.tabuto.jizy.dao.ContactDao#deleteContact(long, com.tabuto.jizy.models.AbstractUser)
	 */
	@Override
	public boolean deleteContact(long id, AbstractUser user) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see com.tabuto.jizy.dao.ContactDao#getAllContact(com.tabuto.jizy.models.AbstractUser)
	 */
	@Override
	public List<ContactModel> getAllContact(AbstractUser user) {
		List<ContactModel> result = new ArrayList<ContactModel>();
		try{
			Connection c = getConnection();
			
			
			PreparedStatement pstat  = 
					c.prepareStatement(
							JDBCConstant.SQL_P_GET_ALL_CONTACT);
			
			pstat.setInt( 1, (int) user.getId());
			ResultSet rs =  pstat.executeQuery();
			
			while(rs!=null && rs.next()){
				ContactModel contact = new ContactModel();
				contact.setId(rs.getInt("id"));
				contact.setName(rs.getString("name"));
				contact.setSurname(rs.getString("surname") );
				result.add(contact);
			}
			rs.close();
			c.close();
		} catch (Exception e){
			throw new RuntimeException("An error occours");
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see com.tabuto.jizy.dao.ContactDao#findContact(com.tabuto.jizy.models.ContactModel, com.tabuto.jizy.models.AbstractUser)
	 */
	@Override
	public List<ContactModel> findContact(ContactModel filters,
			AbstractUser user) {
		// TODO Auto-generated method stub
		return null;
	}

}
